class ArticlesController < ApplicationController
	# CRUD 动作：index，show，new，edit，create，update 和 destroy
	# 安全验证
	# http_basic_authenticate_with name: "sud", password: "123456", except: [:index, :show]
	def index
		@articles = Article.all
	end

	def show
		@article = Article.find(params[:id])
  end

  def new 
  	@article = Article.new 
  end

  def edit
  	@article = Article.find(params[:id])
  end

  def create
  	# 自动映射到对应的数据库字段
  	# 引用的是在 app/models/article.rb 文件中定义的 Article 类
  	@article = Article.new(article_params)

 		# 负责把模型保存到数据库
 		if @article.save
 			# 把页面重定向到 show 动作
	    redirect_to @article
	  else
	    render 'new'
	  end
  end

  def update
  	@article = Article.find(params[:id])
 
	  if @article.update(article_params)
	    redirect_to @article
	  else
	    render 'edit'
	  end
  end

  def destroy
  	@article = Article.find(params[:id])

	  @article.destroy
	  redirect_to articles_path
	end

  private
	  def article_params
	    params.require(:article).permit(:title, :text)
	  end
end
